//1. 安装
//2. 导入
const mongoose = require('mongoose');

//3. 连接数据库
mongoose.connect('mongodb://127.0.0.1:27017/h5220731');

//4. 设置连接回调  schedule
mongoose.connection.on('open', () => {
  //5. 声明结构对象
  let BookSchema = new mongoose.Schema({
    title: String,
    author: String,
    price: Number,
  })
  //6. 声明模型对象
  const BookModel = mongoose.model('book', BookSchema);
  //7. 文档操作
  BookModel.insertMany([{
    title: '水浒传',
    price: 29.9,
    author: '施耐庵'
  }, {
    title: '三国演义',
    price: 69.9,
    author: '罗贯中'
  }, {
    title: '红楼梦',
    price: 16.8,
    author: '曹雪芹'
  }], (err, data) => {
    if(err) {
      console.log(err);
      return;
    }
    console.log(data);
    //8. 关闭链接
    mongoose.connection.close(); 
  });
  
})

mongoose.connection.on('error', () => {

})